package com.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.basic.domain.ApiResult;
import com.basic.domain.PageResult;
import com.demo.pojo.User;
import com.demo.service.IUserService;
import com.demo.service.criteria.UserQueryCriteria;
import com.demo.service.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjy
 * @since 2024-10-10
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/getUserByPage")
    public ApiResult<PageResult<UserDto>> getUserByPage(@RequestBody UserQueryCriteria criteria, Pageable pageable) {
        return ApiResult.ok(userService.queryAll(criteria, pageable));
    }

    @GetMapping("/getUserById")
    public ApiResult<UserDto> getUserById(@RequestParam Long id) {
        return ApiResult.ok(userService.queryById(id));
    }

    @PostMapping("getUserOne")
    public ApiResult<UserDto> getUserOne(@RequestBody UserQueryCriteria criteria) {
        return ApiResult.ok(userService.queryOne(criteria));
    }

    @PostMapping("/saveOrUpdateUser")
    public ApiResult<UserDto> saveOrUpdateUser(@RequestBody UserDto userDto) {
        return ApiResult.ok(userService.saveOrUpdate(userDto));
    }

    @GetMapping("/deleteUserById")
    public ApiResult<Object> deleteUserById(@RequestParam Long id) {
        return ApiResult.ok(StrUtil.format("成功删除{}条数据", userService.deleteId(id)));
    }

    @PostMapping("/deleteUserMore")
    public ApiResult<Object> deleteUserMore(@RequestBody UserQueryCriteria criteria) {
        userService.deleteByMore(criteria);
        return ApiResult.ok("删除成功");
    }

}
